package net.hserver.sync.task;

import cn.hserver.core.interfaces.TaskJob;
import cn.hserver.core.ioc.IocUtil;
import cn.hserver.core.queue.HServerQueue;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import com.github.sardine.DavResource;
import lombok.extern.slf4j.Slf4j;
import net.hserver.sync.config.EventConst;
import net.hserver.sync.config.FileState;
import net.hserver.sync.driver.WebDavDriver;
import net.hserver.sync.entity.SyncWebDavConfigEntity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class TaskLocalRemote implements TaskJob {
    public void copyMode(SyncWebDavConfigEntity arg) {
        WebDavDriver driver = IocUtil.getBean(arg.getId(), WebDavDriver.class);
        List<File> files = FileUtil.loopFiles(arg.getLocalPath());
        for (File file : files) {
            try {
                String remotePath = arg.localToRemotePath(file.getAbsolutePath());
                List<DavResource> list = driver.list(remotePath, 0);
                if (list == null || list.isEmpty() || list.get(0).getContentLength() != file.length()) {
                    HServerQueue.sendQueue(EventConst.LOCAL_CHANGE_EVENT, arg.getId(), FileState.onFileCreate, file.getAbsolutePath(), remotePath);
                    log.info("新同步文件：{}->{}", file.getPath(), remotePath);
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
    }


//    public List<DavResource> getRemoteFiles(WebDavDriver driver, String remotePath) {
//        List<DavResource> data = new ArrayList<>();
//        List<DavResource> list = driver.list(remotePath, 1);
//        for (DavResource davResource : list) {
//            if (!remotePath.endsWith("/")) {
//                remotePath = remotePath + "/";
//            }
//            if (davResource.isDirectory() && !davResource.getPath().equals(remotePath)) {
//                data.addAll(getRemoteFiles(driver, davResource.getPath()));
//            } else {
//                data.add(davResource);
//            }
//        }
//        return data;
//    }


    public void syncMode(SyncWebDavConfigEntity arg) {
        WebDavDriver driver = IocUtil.getBean(arg.getId(), WebDavDriver.class);
        //获取本地文件列表
        List<File> files = FileUtil.loopFiles(arg.getLocalPath());
        //获取远端文件列表
        List<DavResource> remoteFiles = driver.list(arg.getRootRemotePath(), -1);
        for (DavResource remoteFile : remoteFiles) {
            if (!remoteFile.isDirectory()) {
                boolean exist = false;
                for (File file : files) {
                    String local = arg.localToPath(file.getAbsolutePath());
                    String remote = arg.remoteToPath(remoteFile.getPath());
                    if (local.equals(remote)) {
                        exist = true;
                        break;
                    }
                }
                if (!exist) {
                    HServerQueue.sendQueue(EventConst.LOCAL_CHANGE_EVENT, arg.getId(), FileState.onFileDelete, remoteFile.getPath(), remoteFile.getPath());
                }
            } else {
                boolean exist = false;
                for (File file : files) {
                    String local = arg.localToPath(file.getAbsolutePath());
                    String remote = arg.remoteToPath(remoteFile.getPath());
                    if (local.contains(remote)) {
                        exist = true;
                        break;
                    }
                }
                if (!exist) {
                    HServerQueue.sendQueue(EventConst.LOCAL_CHANGE_EVENT, arg.getId(), FileState.onFileDelete, remoteFile.getPath(), remoteFile.getPath());
                }

            }
        }
        copyMode(arg);
    }


    @Override
    public void exec(Object... args) {
        SyncWebDavConfigEntity arg = (SyncWebDavConfigEntity) args[0];
        if (arg.getSyncMode() == SyncWebDavConfigEntity.SyncMode.copy) {
            copyMode(arg);
        } else {
            syncMode(arg);
        }
        log.info(JSONUtil.toJsonStr(arg));
    }
}
